#include "stm32f4xx.h"
#include "ff.h"
#include "flash_manager.h"
#include "ftfile.h"
#include "mymem.h"
#include <stdio.h>


#include "freetype/config/ftstdlib.h"


//int fclose(FILE * /*stream*/)

//FILE *fopen(const char * __restrict /*filename*/,
//                           const char * __restrict /*mode*/)


//size_t fread(void * __restrict /*ptr*/,
//                    size_t /*size*/, size_t /*nmemb*/, FILE * __restrict /*stream*/)

//int fseek(FILE * /*stream*/, long int /*offset*/, int /*whence*/)


//long int ftell(FILE * /*stream*/)


//打开文件
ft_file_struct *ft_if_fopen (const char *filename,const char *mode)
{
    ft_file_struct *file=mycalloc (sizeof (ft_file_struct));
    u8 ret=0;

    if (filename[0]=='0'&&filename[1]==':')
    {
        ret=f_stat (filename,&file->file_info);
        if (ret==FR_OK)
        {
            f_open(&file->file,filename,FA_READ);
        }
        else
        {
            myfree(file);
            file=0;
        }
    }
    else
    {
        if (file->flash.addr= FLASH_FindFile((char *)filename,&file->flash.size),file->flash.addr==0)
        {
            myfree(file);
            file=0;
        }
    }
    return file;
}


//关闭文件
int ft_if_fclose (ft_file_struct *file)
{
    if (file)
    {
        if (file->flash.size==0)
            f_close(&file->file);
        return 0;
    }
    return -1;
}


//读取文件
//缓冲指针，缓冲元素大小，元素个数
unsigned int ft_if_fread (void *ptr,unsigned int size,unsigned int nmemb, ft_file_struct *file)
{
    if (file)
    {
        if (file->flash.size==0)
        {
            UINT rb=0;
            f_read (&file->file,ptr,size*nmemb,&rb);
            return rb;
        }
        else
        {
            u32 read_size=size*nmemb;
            if (read_size>file->flash.size-file->flash.ptr)
                read_size=file->flash.size-file->flash.ptr;
            FLASH_ReadData(ptr,file->flash.ptr+file->flash.addr,size*nmemb);
            file->flash.ptr+=read_size;
            return read_size;
        }
    }
    return 0;
}


//文件偏移
int ft_if_fseek (ft_file_struct *file,long int offset,int whence)
{
    if (file)
    {
        if (file->flash.size==0)
        {
            //文件开头
            if (SEEK_SET==whence)
            {
                if (f_lseek(&file->file,offset)==FR_OK)
                    return 0;
            }
            else if (SEEK_END==whence)
            {
                //这时offset为负数
                offset=file->file_info.fsize+offset;
                if (f_lseek(&file->file,offset)==FR_OK)
                    return 0;
            }
        }
        else
        {
            //文件开头
            if (SEEK_SET==whence)
            {
                if (file->flash.size>offset)
                {
                    file->flash.ptr=offset;
                    return 0;
                }
            }
            else if (SEEK_END==whence)
            {
                if (file->flash.size+offset>0)
                {
                    file->flash.ptr=file->flash.size+offset;
                    return 0;
                }
            }
        }
    }
    return -1;
}


//返回相对与文件开头的偏移
long int ft_if_ftell (ft_file_struct *file)
{
    if (file)
    {
        if (file->flash.size==0)
        {
            return f_tell(&file->file);
        }
        else
        {
            return file->flash.ptr;
        }
    }
    return -1;
}